﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;
using Workflow.Alerts;
using Workflow.Alerts.Utils;

namespace Workflow.Layout.Layouts
{
    public partial class MainLayout : BaseLayout
    {
        #region Events

        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            HideInvalidCss();
            SetNavigationLinks(lnkSettings, lnkProfile, lnkHelp, lnkLogo);
            ShowConfirmation.Value = user.Confirmation.ToString().ToLower();
            lnkProfile.Text = string.Format("{0} {1}", user.FirstName, user.LastName) + " [" + user.Name + "]";
            MunicipalityName.Value = BranchName;

            if (!IsPostBack)
            {
                txtSearch.Text = GetSearchText();
            }

            AddUserAgentBodyClass();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtSearch.Text))
            {
                lblSearchEmptyMsg.Visible = true;
                return;
            }

            Search(txtSearch.Text);
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Logout();
        }
        #endregion

        #region Private Methods

        protected override void SetMenuSources()
        {
            Menu1.DataSource = MenuSource;
            if (user.Type == WorkflowEnums.UserType.Municipality)
            {
                Menu1.AlertCount = Utils.GetMenuStatuses(user.TypeId, null);
            }
            else
            {
                Menu1.AlertCount = Utils.GetMenuStatuses(null, user.TypeId);
            }
        }

        private void HideInvalidCss()
        {
            if (Request.Browser.EcmaScriptVersion.Major <= 0 && Request.Browser.Frames == false && Request.Browser.Type == "Unknown" && Request.Browser.Browser == "Unknown")
            {
                phHideInvalidCss.Visible = false;
            }
        }


        /// <summary>
        /// Hides fontface CSS if OS of user is XP or similar
        /// </summary>
        private void AddUserAgentBodyClass()
        {
            string browserClass = HttpContext.Current.Request.Browser.Browser + HttpContext.Current.Request.Browser.MajorVersion.ToString();
            if (Request.UserAgent.Contains("Chrome"))
            {
                browserClass = "chrome" + HttpContext.Current.Request.Browser.MajorVersion.ToString();
            }
            else if (Request.UserAgent.Contains("Safari"))
            {
                browserClass = "safari" + HttpContext.Current.Request.Browser.MajorVersion.ToString();
            }
            // TODO: This line of code below is written for Sitefinity. Please replace it with a SiteCore-version, uncomment it and remove the line after it.
            //Output.AddClass(this.MasterPageBody, browserClass);
            PageBody.Attributes["class"] += " " + browserClass.ToLower();
        }

        #endregion
    }
}
